import { formatResources } from '@/components/spaces/resources-v2/format';
import { formatIssuesList } from '@/components/spaces/issues/format';

export const formatGroupSpace = space => ({
  id: space.id,
  title: space.title,
  desc: space.desc,
  avatar: space.ufile,
});

export const formatGroup = group => ({
  id: group.id,
  title: group.title,
  desc: group.description,

  joinConditions: group.join_condition || [],
  joinKey: group.key,
  membersCount: group.member_count,

  space: (group.product && group.product.length) ? formatGroupSpace(group.product[0]) : {},

  owner: group.owner || {},
  admins: group.admins || [],

  created_at: group.created_at,
  updated_at: group.updated_at,
});

export const formatGroups = groups => groups.map(group => formatGroup(group));

export const formatMember = member => ({
  id: member.user.id,
  nickname: member.user.nickname,
  avatar: member.user.avatar,

  level: member.role,
  joinConditions: member.join_condition_hash || {},

  created_at: member.created_at,
  updated_at: member.created_at,
});

export const formatMembers = members => members.map(member => formatMember(member));

export const formatSpace = space => ({
  id: space.id,
  title: space.title,
  desc: space.description,
  avatar: space.ufile,
});

export const formatSpaces = spaces => spaces.map(space => formatSpace(space));

export const formatRelatedSpaces = info => info
  .map(item => ({
    id: item.product.id, // 空间id
    index: item.index, // 空间的排序
    title: item.product.title, // 空间名
    avatar: item.product.ufile, // 空间封面
    desc: item.product.description, // 空间描述
    owner: item.product.owner || {}, // 空间拥有者
    open: item.product.is_open, // 空间是否公开
  })).sort((a, b) => Number(a.index) - Number(b.index)); // sort排序

export const formatActivitiesEndTime = (endTime) => {
  const today = new Date(new Date().setHours(0, 0, 0, 0));
  const hh = endTime.getHours();
  const mm = endTime.getMinutes();
  return `${endTime < today ? '昨天' : '今天'} ${hh < 10 ? `0${hh}` : hh}:${mm < 10 ? `0${mm}` : mm}`;
};

export const formatActivities = (activities) => {
  const endTime = formatActivitiesEndTime(new Date(activities.time * 1000)); // ruby传来的时间没有毫秒数
  const docs = activities.merge_count;
  const issues = activities.issue_count;
  const comments = activities.comment_count;
  const docsCount = docs.map(i => i[1]).reduce((a, b) => a + b);
  const issuesCount = issues.map(i => i[1]).reduce((a, b) => a + b);
  const commentsCount = comments.map(i => i[1]).reduce((a, b) => a + b);
  return { docs, issues, comments, docsCount, issuesCount, commentsCount, endTime };
};

export const formatRankingType = (type) => {
  switch (type) {
    case 'docs-comments': return 'snippet_comment';
    case 'issues-comments': return 'issue_comment';
    case 'users-comments': return 'user_comment';
    case 'docs-like': return 'snippet_invote';
    case 'issues-like': return 'issue_invote';
    default:
      console.warn('群组数据排名未传入类型');
      return undefined;
  }
};

export const formatUserRanking = items => items.map(i => ({
  id: i.user.id,
  nickname: i.user.nickname,
  avatar: i.user.avatar,
  commentsCount: i.count,
}));

export const formatRanking = (type, items) => {
  switch (type) {
    case 'docs-comments': return formatResources(items);
    case 'issues-comments': return formatIssuesList(items);
    case 'users-comments': return formatUserRanking(items);
    case 'docs-like': return formatResources(items);
    case 'issues-like': return formatIssuesList(items);
    case 'files': return formatResources(items);
    case 'issues': return formatIssuesList(items);
    default:
      console.warn('群组数据排名未传入类型');
      return undefined;
  }
};

export const formatMemberCSV = (member, score) => {
  const nickname = member.user.nickname;
  const docs = member.collection_info.merge_count;
  const issues = member.collection_info.issue_count;
  const comments = member.collection_info.comment_count;
  const likes = member.collection_info.vote_count;

  const result = {
    用户昵称: nickname,
    ...member.join_condition_hash,
    贡献资源数: docs,
    提出问题数: issues,
    评论回复: comments,
    点赞数: likes,
  };

  if (score) {
    Object.assign(result, {
      评分:
        (Number(docs) * score.docs)
        + (Number(issues) * score.issues)
        + (Number(comments) * score.comments)
        + (Number(likes) * score.like),
    });
  }

  return result;
};

export const formatMembersCSV = (members, score, start, end) => {
  let result = members.map(member => formatMemberCSV(member, score));

  if (score) {
    result = result.sort((a, b) => b['评分'] - a['评分']);
    const proportion = 100 / result[0].评分;
    result = result.map(i => ({
      ...i,
      评分: Math.round(proportion * i.评分),
    }));
  }

  result = result.map(i => ({
    ...i,
    起始日期: (new Date(start)).toLocaleDateString(),
    截止日期: (new Date(end)).toLocaleDateString(),
  }));

  return result;
};

export const formatAnnouncement = item => ({
  id: item.id,
  title: item.title,
  content: item.content,
  author: item.owner || {},

  commentsCount: item.comment_count,

  created_at: item.created_at,
});

export const formatAnnouncements = items => items.map(i => formatAnnouncement(i));
